package com.rlf.sf.legacymapping.fileparse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.rlf.sf.legacymapping.uitls.POIUtils;
import com.rlf.sf.legacymapping.vo.LegacyCategoryVo;
import com.rlf.sf.legacymapping.vo.PermissionVo;
import com.rlf.sf.legacymapping.vo.SectionVo;

/**
 * Created by huangfang on 9/18/2018.
 */
public class FileParse {

    public static void main(String[] args) {
        String filePath = FileParse.class.getClassLoader().getResource("").getPath();
        List<SectionVo> sectionVos = getParseFileData(filePath);
        System.out.println(sectionVos);
        
        sectionVos.forEach(section -> {
        	section.getLegacyPermissionVos().stream().forEach(category -> {
        		System.out.println(category.getLegecyCategroyMessageKey());
        		category.getPermissionVos().forEach(mapping -> {
        			System.out.println(mapping.getLegecyPermissionMessageKey() + " " + mapping.getRbpPermissionMessageKey());
        		});
        		System.out.println("\n");
        	});
        });
    }
    
    
    public static List<SectionVo> getAllModuleTemplatesAsSections () {
    	String filePath = FileParse.class.getClassLoader().getResource("").getPath();
        List<SectionVo> sectionVos = getParseFileData(filePath);
        return sectionVos;
    }
    
    static int lineNumber = 0;


    public static List<SectionVo> getParseFileData(String filePath) {
        Workbook wb = null;
        List<SectionVo> sectionVos = new ArrayList<>();
        sectionVos.add(new SectionVo("ADMINV2_ADMIN_PRIVILEGES"));
        sectionVos.add(new SectionVo("ADMIN_PERMDEFGRP"));
        
        List<String> filePaths = POIUtils.getFileList(new File(filePath));
        SectionVo sectionVo = null;
        for (String fileNamePath : filePaths) {
        	
            try {
                InputStream inputStream = new FileInputStream(fileNamePath);
                wb = new XSSFWorkbook(inputStream);
                Sheet sheet = wb.getSheetAt(0);
                int firstRowIndex = sheet.getFirstRowNum() + 4;
                int lastRowIndex = sheet.getLastRowNum();
               
                for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
                    Row row = sheet.getRow(rIndex);
                    if(row == null) {
                    	continue;
                    }
                    
                    //Check the cell 12 error message
                    Cell c12 = row.getCell(12);
                    if( c12 != null && c12.getStringCellValue().length() > 0) {
                    	continue;
                    }
                    
                    int firstCellIndex = row.getFirstCellNum();
                    int lastCellIndex = row.getLastCellNum();
                    int cIndex = 0;

                    LegacyCategoryVo legacyCategoryVo =  new LegacyCategoryVo();
                    PermissionVo permissionVo  = new PermissionVo();
                    
                    for (cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
                        Cell cell = row.getCell(cIndex);
                        if (cell != null) {
                        	if (cell.toString() == null || "".equals(cell.toString())) {
								break;
							}
                        	String cellValue = cell.toString().trim().replaceAll("\\n", "");
                            if (cIndex == 0) {
                                if (cellValue.toLowerCase().contains("Administrative Privileges".toLowerCase())) {
                                	sectionVo = sectionVos.get(0);
                                } else {
                                	sectionVo = sectionVos.get(1);
                                }
                                sectionVo.setModule(fileNamePath);
                            } else if (cIndex == 2) {
                                permissionVo.setLegecyPermissionType(cellValue);
                            } else if (cIndex == 3) {
                                permissionVo.setLegecyPermissionStringValue(cellValue);
                            } else if (cIndex == 4) {
                                legacyCategoryVo.setLegecyCategroyMessageKey(cellValue);
                            } else if (cIndex == 5) {
                                permissionVo.setLegecyPermissionMessageKey(cellValue);
                            } else if (cIndex == 8) {
                                permissionVo.setRbpPermissionType(cellValue);
                            } else if (cIndex == 9) {
                                permissionVo.setRbpPermissionStringValue(cellValue);
                            } else if (cIndex == 11) {
                                permissionVo.setRbpPermissionMessageKey(cellValue);
                            }
                        } else {
                            break;
                        }
                    }
                    
                    List<LegacyCategoryVo> categories = sectionVo.getLegacyPermissionVos();
                    
                    if(legacyCategoryVo.getLegecyCategroyMessageKey() != null && !legacyCategoryVo.getLegecyCategroyMessageKey().equals("N/A")) {
                    	if(categories.contains(legacyCategoryVo)) {
                    		for (LegacyCategoryVo categoryVo : categories) {
                    			if(categoryVo.getLegecyCategroyMessageKey().equals(legacyCategoryVo.getLegecyCategroyMessageKey())) {
                    				categoryVo.getPermissionVos().add(permissionVo);
//                    				System.out.println(String.format("%s-%s has been exit, ignore add to categories.", ++lineNumber, categoryVo.getLegecyCategroyMessageKey()));
                    				break;
                    			} 
                    		}
                    	} else {
                    		legacyCategoryVo.getPermissionVos().add(permissionVo);
                    		categories.add(legacyCategoryVo);
//                    		System.out.println(String.format("%s-%s not exit, add to categiries.", ++lineNumber, legacyCategoryVo.getLegecyCategroyMessageKey()));
                    	}
                    }
                    	
                }
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return sectionVos;
    }

}

